package com.fjrcloud.service;

import com.fjrcloud.dao.OnTimeRewardDao;
import com.fjrcloud.entity.HouseholdMaster;
import com.fjrcloud.entity.OnTimeReward;
import com.fjrcloud.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by shengjian.lin on 17-2-11.
 */
@Service
public class OnTimeRewardService {

    @Autowired
    private OnTimeRewardDao dao;
    @Autowired
    private HouseholdService service;

    public OnTimeReward get(Integer id){
        return dao.findOne(id);
    }
    public OnTimeReward get(HouseholdMaster master){
        return dao.findByMaster(master);
    }

    public void del(Integer id){
        dao.delete(id);
    }

    public OnTimeReward save(OnTimeReward entity,Integer masterId) throws BusinessException {

        if (masterId == null)
            throw new BusinessException("无认定主体");
        HouseholdMaster master = service.getMaster(masterId);
        if (master == null)
            throw new BusinessException("认定主体无效");

        OnTimeReward old = get(master);

        if (old != null){
            entity.setId(old.getId());
        }
        entity.setMaster(master);

        return dao.save(entity);

    }

}
